package com.longjianghuasheng.logistics.business.release_record

import android.os.Bundle
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseMultiStateFragment
import com.longjianghuasheng.logistics.business.release_record.adapter.ReleaseRecordAdapter
import com.longjianghuasheng.logistics.databinding.FragmentReleaseRecordChildBinding
import com.longjianghuasheng.logistics.eventViewModel
import com.longjianghuasheng.logistics.ext.deleteDialog
import com.longjianghuasheng.logistics.ext.initLinerDecoration
import com.longjianghuasheng.logistics.ext.observer

/**
 * 发布记录子Fragment
 */
class ReleaseRecordChildFragment :
    BaseMultiStateFragment<ReleaseRecordViewModel, FragmentReleaseRecordChildBinding>() {
    override fun isRefresh(): Boolean = true

    override fun isLoadMore(): Boolean = true

    private val releaseRecordAdapter by lazy {

        ReleaseRecordAdapter()
    }

    override fun layoutId(): Int = R.layout.fragment_release_record_child

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        releaseRecordAdapter.run {

            mDatabind.recyclerView.initLinerDecoration(adapter = this)
                .setHasFixedSize(false)

            addChildClickViewIds(R.id.iv_delete, R.id.tv_to_be_agreed)

            setOnItemChildClickListener { _, view, position ->
                mViewModel.id = data[position].id ?: 0
                when (view.id) {
                    R.id.iv_delete -> deleteDialog {

                        mViewModel.pre_del()

                    }
                    R.id.tv_to_be_agreed -> {
                        if (data[position].is_agree == 1) return@setOnItemChildClickListener

                        mViewModel.agree()

                    }
                    else -> {}
                }


            }

        }


    }


    override fun createObserver() {
        super.createObserver()
        mViewModel.result.observer(this) {
            if (mViewModel.page == 1) {
                if (it.data.isNullOrEmpty()) {
                    mStatefulLayout.showEmpty()
                    return@observer
                }
                releaseRecordAdapter.setNewInstance(it.data)

                return@observer
            }

            if (it.data.isNullOrEmpty()) {

                mViewModel.page--
                smartLayout.setNoMoreData(true)

                return@observer
            }
            releaseRecordAdapter.addData(it.data ?: mutableListOf())

        }
        mViewModel.deleteNotify.observer(this) {


            releaseRecordAdapter.removeAt(releaseRecordAdapter.data.indexOfFirst { it.id == mViewModel.id })

        }
        mViewModel.agreeResult.observer(this) {

            releaseRecordAdapter.run {
                val index = data.indexOfFirst { it.id == mViewModel.id }

                data[index].is_agree = 1
                notifyItemChanged(index)
            }

        }

        eventViewModel.onReleaseSearchEvent.observeInFragment(this) {

            mViewModel.keywords = it

            onRefresh()

        }
        eventViewModel.onReleaseStatusEvent.observeInFragment(this) {

            mViewModel.status = it
            onRefresh()
        }
        eventViewModel.onReleaseUpdateEvent.observeInFragment(this) {
            mViewModel.pre_order_list()
        }

    }

    override fun initData() {
        super.initData()
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        mViewModel.pre_order_list()
    }

}